
function Layer(name,index,pulsar)
{
  this.index = index;
  this.name = name;
  this.objects = [];
  this.supporting_objects = [];
  this.parent = pulsar;
  this.ready = 1;
  this.x = pulsar.options['width']/2;
  this.y = pulsar.options['height']/2;
  this.updated = 1;
  this.ctx;
  this.tag;

  this.area = {'x':0,'y':0,'w':0,'h':0};
  this.start_time = 0;
  this.vx = 0;
  this.vy = 0;
  this.iter = 0;
  this.a_iter = 0;
  this.current_angle = 0;
  this.angle = 0;
  this.vangle = 0;
  this.time = 0;
  this.was_r = [];
  this.needClear = true;

  this.getCtx = function()
  {
    return this.ctx;
  }
  this.clear = function()
  {
    if(this.needClear)
    {
      var w = this.parent.options['width'];
      var h = this.parent.options['height'];
      this.getCtx().clearRect(-w,-h,w*2,h*2);
    }
  }

  this.setIndex = function (index)
  {
    if(this.parent.layers[index] != undefined)
    {
      return false;
    }
    else
    {
      this.update();
      this.parent.layers[index] = this;
      delete this.parent.layers[this.index];
      this.index = index;
      this.tag.style.zIndex = index;
    }
    return this;
  }

  this.addObject = function (name,type,is_supporting)
  {
    if(typeof(type) == 'string')
    {
      // check if object with this name exists
      if(name && (this.objects[name] != undefined || this.supporting_objects[name] != undefined))
        return false;
      if(!is_supporting)
      {
        if(name)
        {
          this.objects[name] = new PlObject(name,type,this);
          return this.objects[name];
        }
        else
        {//console.info(this.objects)
          this.objects[this.objects.length] = new PlObject(name,type,this);
         // console.info(this.objects);
         // console.info(this.objects.length);
          return this.objects[this.objects.length-1];
        }
      }
      else
      {
        this.update();
        this.supporting_objects[name] = new PlObject(name,type,this);
        return this.supporting_objects[name];
      }
    }
    else if(typeof(type) == 'object')
    {
      if(!is_supporting)
      {
        this.objects[name] = type.clone();
        return this.objects[name];
      }
      else
      {
        this.supporting_objects[name] =  type.clone();
        return this.supporting_objects[name];
      }
    }
  }

  this.getObject = function(name)
  {
    if(this.objects[name]) return this.objects[name];
    else if(this.supporting_objects[name]) return this.supporting_objects[name];
  }
  this.moveTo = function(x,y,time)
  {
    if(!time) console.error('U mast set time for function moveTo');
    this.update();
    var interval = this.parent.options['interval'];
    this.start_time = this.parent.time;
    var dx = this.x - x;
    var dy = this.y - y;
    this.vx = -Math.round((dx/time) * interval);
    this.vy = -Math.round((dy/time) * interval);
    this.iter = Math.round(time / interval);
    //this.getCtx().translate(this.x, this.y);
    return this;
  }
  this.rotate = function(angle,time)
  {
    this.update();
    var dangle = this.angle - angle;
    var interval = this.parent.options['interval'];
    this.vangle = (dangle/time) * interval;
    this.a_iter = Math.round(time / interval);
    return this;
  }
  this.tryReady = function()
  {
          for( o in this.objects)
          {
                  if(this.objects[o].ready != 1)
                  {
                          this.ready = 0;
                          return false;
                  }
          }
          for( o in this.supporting_objects)
          {
                  if(this.supporting_objects[o].ready != 1)
                  {
                          this.ready = 0;
                          return false;
                  }
          }

          this.ready = 1;
          this.parent.tryReady();
          return this;
  }
  this.update = function()
  {
          this.updated = 1;
  }
  this.checkUpdate = function()
  {
    if(this.updated != 1)
    {
      if(this.iter > 0)
      {
        this.update();
        return true;
      }
      if(this.a_iter > 0)
      {
        this.update();
        return true;
      }
    
      for( o in this.objects)
      {
        if(this.objects[o].checkUpdate())
        return true;
      }
    
      for( o in this.supporting_objects)
      {
        if(this.supporting_objects[o].checkUpdate())
        return true;
      }
    }
  }
  this.draw = function ()
  {
    this.checkUpdate();
    if(this.updated == 1)
    {
      this.clear();
      if(this.iter > 0)
      {
        this.x += this.vx;
        this.y += this.vy;
        this.iter--;
      }
      if(this.time != this.parent.time)
      {
              if(this.a_iter > 0)
              {
                      this.angle -= this.vangle;
                      this.a_iter--;
              }
      }
      this.getCtx().translate(this.x, this.y);
      if(this.angle != this.current_angle)
      {
              //this.getCtx().save();
              //console.info(Math.round(this.x+this.w/2), Math.round(this.y+this.h/2));
              //this.getCtx().translate(this.x+this.parent.options['width']/2, this.y+this.parent.options['height']/2);
              this.getCtx().rotate(-Math.PI*((this.current_angle-this.angle)/180));
              this.current_angle = this.angle;
              //this.was_r['x'] = this.x;
              //this.was_r['y'] = this.y;
              //this.x = -(this.parent.options['width']/2);
              //this.y = -(this.parent.options['height']/2);
      }

      for( o in this.objects)
      {
              this.objects[o].draw();
      }

      this.getCtx().translate(-this.x, -this.y);
    }
    this.updated = 0;

  }
}

